package common;

import common.Enums.ProtocolState;

/**
 * @author leah 
 * Checks to see if the transition between states follows the DFA that we 
 * designed
 */
public class Transition
{
    /**
     * Default constructor
     */
    private Transition()
    {
    }

    /**
     * Returns Whether or not the specified state transition from previous to current is allowed.
     * @return True if transition was valid; false, otherwise.
     */
    public static boolean TransitionCheck(ProtocolState previous, ProtocolState current)
    {
        // determining if the current state came from the correct previous state
        switch (current)
        {
            case INITIAL:
                if (previous == null)
                    return true;
                else if (previous == ProtocolState.LOGOUT)
                    return true;
                break;
            case AUTHENTICATION:
                if (previous == ProtocolState.INITIAL)
                    return true;
                else if (previous == ProtocolState.AUTHENTICATION)
                    return true;
                break;
            case CONNECTED:
                if (previous == ProtocolState.AUTHENTICATION)
                    return true;
                else if (previous == ProtocolState.CONNECTED)
                    return true;
                else if (previous == ProtocolState.COMPOSITION)
                    return true;
                break;
            case COMPOSITION:
                if (previous == ProtocolState.COMPOSITION)
                    return true;
                else if (previous == ProtocolState.CONNECTED)
                    return true;
                break;
            case LOGOUT:
                if (previous == ProtocolState.AUTHENTICATION)
                    return true;
                else if (previous == ProtocolState.CONNECTED)
                    return true;
                else if (previous == ProtocolState.COMPOSITION)
                    return true;
                else if (previous == ProtocolState.INITIAL)
                    return true;
                break;
            default:
                return false;
        }
        return false;
    }
}